package com.sau.pixelcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sau.pixelcart.common.context.UserContext;
import com.sau.pixelcart.domain.entity.InventoryAlert;
import com.sau.pixelcart.domain.entity.ProductInventory;
import com.sau.pixelcart.mapper.InventoryAlertMapper;
import com.sau.pixelcart.mapper.ProductInventoryMapper;
import com.sau.pixelcart.service.IInventoryAlertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sau.pixelcart.service.IProductInventoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存预警事件表 服务实现类
 * </p>
 *
 * @author Inton
 * @since 2025-03-08
 */
@Service
public class InventoryAlertServiceImpl extends ServiceImpl<InventoryAlertMapper, InventoryAlert> implements IInventoryAlertService {

    @Autowired
    ProductInventoryMapper productInventoryMapper;
    @Autowired
    private InventoryAlertMapper inventoryAlertMapper;

    /**
     * 检查库存警报信息
     * @Param productId
     */
    @Override
    @Transactional
    public void checkStockWarning(String productId) {
        //查询当前库存信息
        LambdaQueryWrapper<ProductInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductInventory::getProductId, productId);
        ProductInventory productInventory = productInventoryMapper.selectOne(queryWrapper);

        if(productInventory.getStock() <= productInventory.getLowStockThreshold()){

            //判断该预警是否已经存在
            LambdaQueryWrapper<InventoryAlert> inventoryAlertQueryWrapper = new LambdaQueryWrapper<>();
            inventoryAlertQueryWrapper.eq(InventoryAlert::getProductId, productId);
            inventoryAlertQueryWrapper.eq(InventoryAlert::getStatus, "pending");
            InventoryAlert lastInventoryAlert = inventoryAlertMapper.selectOne(inventoryAlertQueryWrapper);
            if(lastInventoryAlert != null){
                //该商品有未处理的预警信息，更新为当前库存
                inventoryAlertMapper.updateCurrentStock(productInventory.getProductId(),productInventory.getStock());
            }else{
                //若该商品没有未处理的预警，则触发库存预警
                InventoryAlert inventoryAlert = new InventoryAlert();
                inventoryAlert.setProductId(productId);
                inventoryAlert.setTriggerTime(LocalDateTime.now());
                inventoryAlert.setCurrentStock(productInventory.getStock());
                inventoryAlert.setThreshold(productInventory.getLowStockThreshold());
                inventoryAlert.setStatus("pending");
                this.save(inventoryAlert);
            }
        }
    }

    /**
     * 设置库存预警状态为已处理
     * @param productInventory
     */
    @Override
    public void processAlert(ProductInventory productInventory) {
        //处理库存预警
        String productId = productInventory.getProductId();
        String status = "processed";
        String userId = UserContext.getUser();
        LocalDateTime processedTime = LocalDateTime.now();

        inventoryAlertMapper.processAlert(productId,status,userId,processedTime);
    }

}
